<template>
	<view>
		<my-search @click="gotoSearch"></my-search>
		<!-- <view class="scroll-view-container">
			左侧的滚动视图区域
			<scroll-view class="left-scroll-view" scroll-y :style="{height:wh+'px'}">
				<block v-for="(item,i) in cateList" :key="i">
					<view :class="['left-scroll-view-item', active == i ? 'active' : '']" @click="activeChanged(i)">
						{{item.cat_name}}
					</view>
				</block>
			</scroll-view>

			右侧的滚动视图区域
			<scroll-view class="right-scroll-view" scroll-y :style="{height:wh+'px'}" :scroll-top="scrollTop">
				<view class="cate-lv2" v-for="(item,i) in cateLevel2" :key="i">
					<view class="cate-lv2-title">
						{{item.cat_name}}
					</view>
					动态渲染三级分类的列表数据
					<view class="cate-lv3-list">
						三级分类Item项
						<view class="cate-lv3-item" v-for="(item2,i2) in item.children" :key="i2"
							@click="gotoGoodsList(item2)">
							图片
							<image :src="item2.cat_icon.replace('dev','web')"></image>
							//下面这行代码需要注释
							<image :src="item2.cat_urlgg"></image>
							<text>{{item2.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>

		</view> -->
		<!-- 左侧分类盒子 -->
		<view class="scroll-view-container">
			<!-- 左侧的滚动视图区域 -->
			<scroll-view class="left-scroll-view" scroll-y :style="{height:wh+'px'}">

				<block v-for="item in typeData" :key="item.id">
					<view :class="['left-scroll-view-item', active == item.typeId ? 'active' : '']"
						@click="activeChanged(item.typeId)">
						{{item.typeName}}
					</view>
				</block>
			</scroll-view>

			<!-- 商品图片盒子 -->
			<scroll-view class="right-scroll-view" scroll-y :style="{height:wh+'px'}" :scroll-top="scrollTop">
				<view class="cate-box" v-for="item in showData" @click="goDetailPage(item)" :key="item.id">
					<image class="cate-image" style="width: 100px; height: 100px;"
						:src="`http://localhost:3000/${item.goodsImage}`" alt="">
					</image>
					<view class="text-box">
						<view class="goodsName">
							名称：{{item.goodsName}}
						</view>
						<view class="goodsName">
							库存：{{item.goodsCount}}
						</view>
						<view class="goods-dec">
							描述：{{item.goodsDetail}}
						</view>
						<view class="goodsName">
							价格：<text style="color: red;font-weight:700;">￥</text>{{item.goodsPrice}}元
						</view>
					</view>
				</view>
			</scroll-view>
		</view>

	</view>
</template>

<script>
	import {
		getMockData
	} from '../../utils/mock.js'
	export default {
		data() {
			return {
				// 窗口的可用高度 = 屏幕高度 - navigationBar高度 - tabBar高度
				wh: 0,
				// 分类数据列表
				cateList: [],
				// 当前选择的索引，默认让第一项选中
				active: 0,
				// 左侧导航滚动条
				scrollTop: 0,
				// 商品数据
				showData: [],
				// 商品分类数据
				typeData: []
			};
		},
		onLoad() {
			// 获取当前系统的信息
			const sysInfo = uni.getSystemInfoSync()
			// 为wh窗口可用高度动态赋值
			this.wh = sysInfo.windowHeight
			// 获取cateList数据
			this.getCateList()
			this.getShopGoods()
			this.getGoodType()
		},
		methods: {
			// 获取商品分类
			getGoodType() {
				uni.request({
					url: 'http://127.0.0.1:3000/category',
					method: 'POST',
					timeout: 5000,
					success: (res) => {
						res = res.data
						if (res.status == 200) {
							this.typeData = res.dataAllMsg
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'error',
							})
						}
					},
					fail: (res) => {
						console.log(res);
						uni.showToast({
							title: '获取数据失败',
							icon: 'error',
						})
					}
				})
			},

			// 获取商品列表
			getShopGoods(typeId) {
				uni.request({
					url: 'http://127.0.0.1:3000/goods',
					method: 'POST',
					timeout: 5000,
					data: {
						typeId
					},
					success: (res) => {
						res = res.data
						if (res.status == 200) {
							console.log(res);
							if (res.dataAllMsg) {
								this.showData = res.dataAllMsg
							} else {
								this.showData = res.data
							}
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'error',
							})
						}
					},
					fail: (res) => {
						uni.showToast({
							title: '获取数据失败',
							icon: 'error',
						})
					}
				})
			},

			// cateList获取数据
			async getCateList() {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/categories')
				// 判断是否获取失败
				if (res.meta.status !== 200) return uni.$showMsg()
				// 转存数据
				this.cateList = res.message
			},


			// 改变active的值
			activeChanged(index) {
				this.active = index
				// 让 scrollTop 的值在 0 与 1 之间切换
				this.scrollTop = this.scrollTop === 0 ? 1 : 0
				// console.log(this.scrollTop);
				this.getShopGoods(index)
			},

			// 点击商品盒子跳转商品详情页面
			goDetailPage(item){
				console.log(item);
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?id=' + item.id
				})
			},

			// 点击商品详情列表触发
			gotoGoodsList(item2) {
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?cid=' + item2.cat_id
				})
			},

			// 点击到分包中的搜索页面
			gotoSearch() {
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			},
		},

		computed: {
			// 获取二级分类数据
			cateLevel2() {
				return this.cateList[this.active]?.children
			}
		}
	}
</script>

<style lang="scss">
	.scroll-view-container {
		display: flex;


		.left-scroll-view {
			width: 120px;

			.left-scroll-view-item {
				line-height: 60px;
				background-color: #f7f7f7;
				text-align: center;
				font-size: 12px;

				// 激活项的样式
				&.active {
					background-color: #ccc;
					position: relative;

					// 渲染激活项左侧的红色指示边线
					&::before {
						content: '';
						display: block;
						width: 3px;
						height: 30px;
						background-color: #c00000;
						position: absolute;
						left: 0;
						top: 50%;
						transform: translateY(-50%);
					}
				}
			}

			.showBox {
				display: felx;
			}

			.showItem {
				img {
					width: 200px;
				}

				width: 200px;
				height: 300px;
			}




		}

		.right-scroll-view {

			.cate-box {
				display: flex;
				width: calc(100vw - 120px);
				height: 100px;
				overflow: hidden;
				padding: 10px;
				border: #ccc solid 1px;

				.text-box {
					width: calc(100% - 120px);
					flex: 1;
					margin: 0 10px;
					display: flex;
					flex-direction: column;
					justify-content: space-around;

					view {
						font-size: 14px;
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;
					}



				}


			}
		}
	}
</style>
